/**
    Dialog dialogs=new UtiDialogsFactory(ActBarcode.this);
    dialogs.CreateLoadingDialog();弹出
    dialogs.setDialogDismiss();销毁
 */

package com.pic4493.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

public class UtiDialog
{
   private UtiDialog(){}
   
   private static class DialogBuilderInstance
   {
	  private static final UtiDialog instance = new UtiDialog();
   }

   public static UtiDialog getInstance()
   {
	  return DialogBuilderInstance.instance;
   }

   private AlertDialog dialog;
   private AlertDialog.Builder builder;
   private ProgressDialog mDialog;

   private static final int loadingDialogNoTitle = 0;
   private static final int loadingDialogTitle = 1;

   // private static final int okAlertDialog=0;
   // private static final int okcancelAlertDialog=1;

   /**
    * 创建一个带有OK按钮的提示窗
    * 
    * @param title
    *           标题
    * @param message
    *           信息
    */
   public void ShowOKAlertDialog(Context context, String title, String message)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setMessage(message);
	  builder.setPositiveButton("确定", new OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {
			dialog.dismiss();
		 }
	  });
	  builder.create().show();
   }

   /**
    * 创建一个带有OK按钮的提示窗
    * 
    * @param title
    *           标题
    * @param message
    *           信息
    */
   public void ShowOKAlertDialog(Context context, String title, String message, DialogInterface.OnClickListener listener)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setMessage(message);
	  builder.setPositiveButton("确定", listener);
	  builder.create().show();
   }

   /**
    * 创建一个带有OK按钮的提示窗(屏蔽返回键和搜索键)
    * 
    * @param title
    *           标题
    * @param message
    *           信息
    */
   public void ShowOKAlertDialogEX(final Context context, String title, String message, DialogInterface.OnClickListener listener)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setMessage(message);
	  builder.setPositiveButton("确定", listener);
	  builder.setCancelable(false);
	  builder.setOnKeyListener(new DialogInterface.OnKeyListener()
	  {
		 @Override
		 public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
		 {
			if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_SEARCH)
			{

			   return true;
			}
			else
			{
			   return false;
			}
		 }
	  });
	  builder.create().show();
   }

   /**
    * 创建一个带有OK,Cancel按钮的提示窗
    * 
    * @param title
    *           标题
    * @param message
    *           信息
    * @param OKButtonOnClickListener
    *           OK按钮的监听器
    */
   public void ShowOKCancelAlertDialog(Context context, String title, String message, OnClickListener OKButtonOnClickListener)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setMessage(message);
	  builder.setPositiveButton("确定", OKButtonOnClickListener);
	  builder.setNegativeButton("取消", new OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {

			dialog.dismiss();
		 }
	  });
	  builder.create().show();
   }

   /**
    * 创建一个带两个自定义按钮的对话框
    * 
    * @param title
    *           标题
    * @param message
    *           正文内容
    * @param LeftBtnString
    *           左侧按钮文字
    * @param LeftBtnOnClickListener
    *           左侧按钮事件
    * @param RightBtnString
    *           右侧按钮文字
    * @param RightBtnOnClickListener
    *           右侧按钮事件
    */
   public void ShowCustomButtonAlertDialog(Context context, String title, String message, String LeftBtnString, OnClickListener LeftBtnOnClickListener, String RightBtnString,
		 OnClickListener RightBtnOnClickListener)

   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setMessage(message);
	  builder.setPositiveButton(LeftBtnString, LeftBtnOnClickListener);
	  builder.setNegativeButton(RightBtnString, RightBtnOnClickListener);
	  builder.create().show();
   }

   public void ShowItemsDialog(Context context, String[] items, DialogInterface.OnClickListener onClickListener)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setItems(items, onClickListener);
	  builder.create().show();
   }
   
   public void ShowItemsDialogWithTitle(Context context, String[] items,String title ,DialogInterface.OnClickListener onClickListener)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setTitle(title);
	  builder.setItems(items, onClickListener);
	  builder.create().show();
   }

   public void ShowSingleSelectItemDialog(Context context, String[] items, int seletItem, DialogInterface.OnClickListener onClickListener)
   {
	  builder = new AlertDialog.Builder(context);
	  builder.setSingleChoiceItems(items, seletItem, onClickListener);
	  builder.create().show();
   }

   /**
    * 
    * @param title
    *           标题，如果没有则留null或者""
    * @param view
    *           需要加载的自定义布局
    * @param height
    *           高度占整个屏幕的百分比 如90代表90%
    * @param width
    *           宽度占整个屏幕的百分比
    */
   public void ShowCustomViewDialog(Context context, String title, View view, int height, int width)
   {
	  dialog = new AlertDialog.Builder(context).create();
	  if (title == null || title.equals(""))
	  {
		 dialog.setView(view);
	  }
	  else
	  {
		 dialog.setTitle(title);
		 dialog.setView(view);
	  }

	  DisplayMetrics dm = new DisplayMetrics();
	  Activity act = (Activity) context;
	  act.getWindowManager().getDefaultDisplay().getMetrics(dm);
	  WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
	  params.width = (dm.widthPixels * (width + 2)) / 100;
	  params.height = (dm.heightPixels * (height + 2)) / 100;
	  dialog.getWindow().setAttributes(params);
	  dialog.show();
   }

   /**
    * 
    * @param title
    *           标题，如果没有则留null或者""
    * @param view
    *           需要加载的自定义布局
    * @param height
    *           高度占整个屏幕的百分比 如90代表90%
    * @param width
    *           宽度占整个屏幕的百分比
    */
   public void ShowCustomViewDialogWithOKCancel(Context context, String title, View view, int height, int width, DialogInterface.OnClickListener clickListener)
   {
	  dialog = new AlertDialog.Builder(context).create();
	  if (title == null || title.equals(""))
	  {
		 dialog.setView(view);
	  }
	  else
	  {
		 dialog.setTitle(title);
		 dialog.setView(view);
	  }
	  dialog.setButton("确定", clickListener);
	  dialog.setButton2("取消", new DialogInterface.OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {

			dialog.dismiss();
		 }
	  });
	  dialog.show();
	  DisplayMetrics dm = new DisplayMetrics();
	  Activity act = (Activity) context;
	  act.getWindowManager().getDefaultDisplay().getMetrics(dm);
	  WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
	  params.width = (dm.widthPixels * (width + 2)) / 100;
	  params.height = (dm.heightPixels * (height + 2)) / 100;
	  dialog.getWindow().setAttributes(params);
   }

   /**
    * 
    * @param title
    *           标题，如果没有则留null或者""
    * @param view
    *           需要加载的自定义布局
    */
   public void ShowCustomViewDialogWithOKCancel(Context context, String title, View view, DialogInterface.OnClickListener clickListener)
   {
	  dialog = new AlertDialog.Builder(context).create();
	  if (title == null || title.equals(""))
	  {
		 dialog.setView(view);
	  }
	  else
	  {
		 dialog.setTitle(title);
		 dialog.setView(view);
	  }
	  dialog.setButton("确定", clickListener);
	  dialog.setButton2("取消", new DialogInterface.OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {

			dialog.dismiss();
		 }
	  });
	  dialog.show();
   }

   /**
    * 关闭警告对话框
    */
   public void DismissAlertDialog()
   {
	  try
	  {
		 if (dialog.isShowing())
		 {
			dialog.dismiss();
			dialog = null;
		 }
	  }
	  catch (Exception e)
	  {
		 Log.e("ERROR", "UtiDialogsFactory.DismissAlertDialog()", e);
	  }

   }

   public void ShowToastInfoShort(Context context, String string)
   {
	  Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
   }

   public void ShowToastInfoLong(Context context, String string)
   {
	  Toast.makeText(context, string, Toast.LENGTH_LONG).show();
   }

   /**
    * 关闭loading对话框
    */
   public void DismissLoadingDialog()
   {
	  try
	  {
		 if (mDialog!=null&&mDialog.isShowing())
		 {
			mDialog.dismiss();
			mDialog = null;
		 }
	  }
	  catch (Exception e)
	  {
		 Log.e("ERROR", "UtiDialogsFactory.DismissLoadingDialog()", e);
	  }

   }

   /**
    * 创建一个没有标题栏的loading窗口
    * 
    * @param message
    *           信息
    */
   public void ShowLoadingDialogNoTitle(Context context, String message)
   {
	  CreateLoadingDialog(context, loadingDialogNoTitle, message, null, false);
   }

   /**
    * 
    * @param context
    * @param message
    * @param title
    */
   public void ShowLoadingDialog(Context context, String message, String title)
   {
	  CreateLoadingDialog(context, loadingDialogTitle, message, title, false);
   }

   /**
    * 创建一个没有标题栏的loading窗口,此方法使用返回键会关闭整个页面而不是dialog
    * 
    * @param context
    * @param message
    */
   public void ShowLoadingDialogNoTitleEX(Context context, String message)
   {
	  CreateLoadingDialog(context, loadingDialogNoTitle, message, null, true);
   }

   /**
    * 
    * @param context
    * @param id
    * @param message
    * @param title
    * @param isEx
    *           判断需不需要屏蔽系统dialog按键设置。用于返回键关闭页面
    */
   protected void CreateLoadingDialog(final Context context, int id, String message, String title, boolean isEx)
   {
	  mDialog = new ProgressDialog(context);
	  mDialog.setCancelable(true);
	  mDialog.setIndeterminate(true);
	  switch (id)
	  {
		 case loadingDialogTitle:
			mDialog.setTitle(title);
			mDialog.setMessage(message);

			break;
		 case loadingDialogNoTitle:
			mDialog.setMessage(message);
			mDialog.setIndeterminate(true);
			break;
	  }
	  if (isEx)
	  {
		 mDialog.setOnKeyListener(new DialogInterface.OnKeyListener()
		 {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
			{
			   if (keyCode == KeyEvent.KEYCODE_BACK)
			   {
				  dialog.dismiss();
				  ((Activity) context).finish();
				  mDialog = null;
				  return true;
			   }
			   else if (keyCode == KeyEvent.KEYCODE_SEARCH)
			   {
				  return true;
			   }
			   else
			   {
				  return false;
			   }
			}
		 });
	  }
	  mDialog.show();
   }
}
